package com.hw.core.system.controller;

import com.hw.core.system.entity.Permission;
import com.hw.core.system.service.PermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("list")
    @RequiresPermissions(value = "资源查询")
    public String list(Model model) {
        model.addAttribute( "permissions", permissionService.findAll() );

        return "permission/listPermission";
    }


    @PostMapping("add")
    @ResponseBody
    @RequiresPermissions(value = "资源新增")
    public String add(Permission permission) {
        if (permissionService.add( permission ) != null) {
            return "添加成功";
        }
        return "添加失败";
    }

    @GetMapping("add")
    @RequiresPermissions(value = "资源新增")
    public String add() {
        return "permission/addPermission";
    }

    @GetMapping("edit")
    @RequiresPermissions(value = "资源修改")
    public String edit(Integer id, Model model) {
        model.addAttribute( "permission", permissionService.getPermissionById( id ) );
        return "permission/editPermission";

    }

    @PostMapping("edit")
    @RequiresPermissions(value = "资源修改")
    @ResponseBody
    public String edit(Permission permission) {
        permissionService.update( permission );
        return "修改成功";
    }

    @PostMapping("delete")
    @RequiresPermissions(value = "资源删除")
    @ResponseBody
    public String delete(Integer id) {
        permissionService.delete( id );
        return "删除成功";


    }
}
